﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Portal.Model;
using Portal.Presentation.Tests.Units;
using FluentAssertions;
using Portal.Presentation.Models;
using Portal.Core.Adapters;
using Portal.Presentation.Converters;

namespace Portal.Presentation.Converters.Tests.Units
{
	[TestClass]
	public class UserViewModelConverterTests : BaseTests
	{
		#region Fields
		private IUserAdapter _adapter;
		#endregion

		protected override void InitializeTest()
		{
			_adapter = Mocks.DynamicMock<IUserAdapter>();
		}

		#region ToUserInfo

		[TestMethod]
		public void ToUserInfo_ModelIsNull_OnlyUserIsSet()
		{
			var user = Mocks.Stub<IUser>();
			Mocks.ReplayAll();

			var converter = GetConverter();
			var result = converter.ToUserInfo(null, user);

			result.Should().NotBeNull();
			result.User.Should().Be(user);
		}

		[TestMethod]
		public void ToUserInfo_ProperInput_Success()
		{
			var user = Mocks.Stub<IUser>();

			var model = new UserInfoDataModel()
			{
				FirstName = "first nname",
				LastName = "last name",
				PrimaryEmail = "primary e-mail",
				SecondaryEmail = "secondary e-mail",
				PrimaryPhone = "primary phone",
				SecondaryPhone = "secondary phone"
			};

			Mocks.ReplayAll();

			var converter = GetConverter();
			var result = converter.ToUserInfo(model, user);

			result.Should().NotBeNull();

			result.PrimaryEmail.Should().NotBeNull();
			result.PrimaryEmail.Value.Should().Be(model.PrimaryEmail);
			result.PrimaryEmail.Type.Should().Be(ContactInfoType.Email);
			result.PrimaryEmail.IsDefault.Should().Be(true);

			result.SecondaryEmail.Should().NotBeNull();
			result.SecondaryEmail.Value.Should().Be(model.SecondaryEmail);
			result.SecondaryEmail.Type.Should().Be(ContactInfoType.Email);
			result.SecondaryEmail.IsDefault.Should().Be(false);

			result.PrimaryPhone.Should().NotBeNull();
			result.PrimaryPhone.Value.Should().Be(model.PrimaryPhone);
			result.PrimaryPhone.Type.Should().Be(ContactInfoType.Phone);
			result.PrimaryPhone.IsDefault.Should().Be(true);
			
			result.SecondaryPhone.Should().NotBeNull();
			result.SecondaryPhone.Value.Should().Be(model.SecondaryPhone);
			result.SecondaryPhone.Type.Should().Be(ContactInfoType.Phone);
			result.SecondaryPhone.IsDefault.Should().Be(false);

			result.User.FirstName.Should().Be(model.FirstName);
			result.User.LastName.Should().Be(model.LastName);
		}

		[TestMethod]
		public void ToUserInfo_ContactInfoValueIsNull_ContactInfoIsNull()
		{
			var model = new UserInfoDataModel() ;
			Mocks.ReplayAll();

			var converter = GetConverter();
			var result = converter.ToUserInfo(model, null);

			result.Should().NotBeNull();

			result.PrimaryEmail.Should().BeNull();
			result.SecondaryEmail.Should().BeNull();
			result.PrimaryPhone.Should().BeNull();
			result.SecondaryPhone.Should().BeNull();
		}

		#endregion ToUserInfo

		#region ToModel
		
		[TestMethod()]
		public void ToModel_UserInfoIsNull_ReturnsNull()
		{
			Mocks.ReplayAll();

			var converter = GetConverter();
			var result = converter.ToModel(null);

			result.Should().BeNull();
		}

		[TestMethod()]
		public void ToModel_ValidUserInfo_ReturnsModel()
		{
			var user = Mocks.DynamicMock<IUserInfo>();
			Mocks.ReplayAll();

			var converter = GetConverter();
			var result = converter.ToModel(user);

			result.Should().NotBeNull();
		} 

		#endregion

		private UserViewModelConverter GetConverter()
		{
			return new UserViewModelConverter(_adapter);
		}

		
	}
}